﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace Starry.SmartAjax
{
    [Serializable]
    public class BizResult
    {
        public BizResult() : this(true) { }
        public BizResult(bool isSuccess) : this(isSuccess, null as IList<string>) { }
        public BizResult(bool isSuccess, string message) : this(isSuccess, new string[] { message }) { }
        public BizResult(bool isSuccess, IList<string> messages) : this(isSuccess, messages, new object()) { }
        public BizResult(bool isSuccess, IList<string> messages, object entity)
        {
            this.IsSuccess = isSuccess;
            this.Messages = messages ?? new List<string>();
            this.Entity = entity;
        }

        public bool IsSuccess { set; get; }
        public IList<string> Messages { set; get; }
        public string Message { get { return this.Messages.FirstOrDefault() ?? string.Empty; } }
        public object Entity { set; get; }

        public virtual string ToResponseString()
        {
            return new JavaScriptSerializer().Serialize(this);
        }
    }

    [Serializable]
    public class BizResult<TEntity>
        : BizResult
    {
        public BizResult() : this(true) { }
        public BizResult(bool isSuccess) : this(isSuccess, null as IList<string>) { }
        public BizResult(bool isSuccess, string message) : this(isSuccess, new string[] { message }) { }
        public BizResult(bool isSuccess, IList<string> messages) : this(isSuccess, messages, default(TEntity)) { }
        public BizResult(bool isSuccess, IList<string> messages, TEntity entity) : base(isSuccess, messages, entity) { }

        new public TEntity Entity
        {
            set { base.Entity = value; }
            get { return (TEntity)base.Entity; }
        }
    }
}